#!/data/Software/mydan/perl/bin/perl -I/data/Software/mydan/Connector/lib -I/data/Software/mydan/Connector/private/lib
use strict;
use warnings;
use Logs;

$| ++;

=head1 SYNOPSIS

    id => 1,
    logs => 日志对象

=cut

my ( $nodeinfo, %env );
BEGIN{
    use Configini;
    $nodeinfo = Configini::get( 'nodeinfo' );
    %env = Configini::env( 'nodeinfoenv' );
};


return sub
{
    my ( %param )= @_;
    my ( $id, $logs ) = @param{qw(id logs)};

    $logs = Logs->new( 'code.nodeinfo' ) unless $logs;

    $logs->die( "id not a number" ) unless $id =~ /^\d+$/;

    my $ua = LWP::UserAgent->new;
    $ua->default_header( %env ) if %env;
    my $res = $ua->get( "$nodeinfo$id" );
    if($res->is_success)
    {
        my $v = eval{decode_json $res->decoded_content};
        if( $v->{stat} && ref $v->{data} eq 'ARRAY' )
        {
            map{ $_->{name} = "unkown" if $_->{name} !~ /^[a-zA-Z0-9\.\-_]$/ }@{$v->{data}};
            return @{$v->{data}};
        }
    }
    #TODO die
    die "die";
}
